package cn.zhaoguang.usedcar.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zhaoguang.usedcar.common.http.ResultVO;
import cn.zhaoguang.usedcar.common.page.PageRequest;
import cn.zhaoguang.usedcar.common.page.PageResult;
import cn.zhaoguang.usedcar.common.util.PageHelperUtil;
import cn.zhaoguang.usedcar.common.util.PasswordUtil;
import cn.zhaoguang.usedcar.common.util.RsaEncryptUtil;
import cn.zhaoguang.usedcar.main.entity.User;
import cn.zhaoguang.usedcar.main.entity.dto.SellerDTO;
import cn.zhaoguang.usedcar.main.entity.dto.UserDTO;
import cn.zhaoguang.usedcar.main.entity.vo.UserAddVO;
import cn.zhaoguang.usedcar.main.entity.vo.UserUpdateInfoVO;
import cn.zhaoguang.usedcar.main.entity.vo.UserUpdatePasswordVO;
import cn.zhaoguang.usedcar.main.mapper.UserMapper;
import cn.zhaoguang.usedcar.main.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/5 15:32
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultVO add(UserAddVO userAddVO) {
        User user = userMapper.findUserByPhone(userAddVO.getPhone());
        if (ObjectUtil.isEmpty(user)) {
            //获取RSA加密的密码并解密
            String decodePassword = RsaEncryptUtil.privateKeyDecrypt(userAddVO.getPassword());

            //获取加密盐
            String salt = PasswordUtil.getSalt();
            //使用MD5加密密码用于存入数据库
            String password = PasswordUtil.encode(salt, decodePassword);

            User u = new User();
            u.setPhone(userAddVO.getPhone());
            u.setPassword(password);
            u.setEncryptSalt(salt);
            u.setCreateTime(DateUtil.date());
            int result = userMapper.insertSelective(u);
            if (result > 0) {
                return ResultVO.success("添加成功");
            }
            return ResultVO.error("添加失败");
        }
        return ResultVO.error("添加失败! 手机号已存在");
    }

    @Override
    public int delete(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String updateInfo(UserUpdateInfoVO userUpdateInfoVO) {
        User user = new User();
        BeanUtil.copyProperties(userUpdateInfoVO, user);

        int result = userMapper.updateByPrimaryKeySelective(user);
        if (result > 0) {
            return "信息修改成功";
        }
        return "信息修改失败";
    }

    @Override
    public String updatePassword(UserUpdatePasswordVO userUpdatePasswordVO) {
        Integer id = userUpdatePasswordVO.getId();
        User user = userMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(user)) {
            //旧密码
            String oldPassword = userUpdatePasswordVO.getOldPassword();
            //对比密码
            boolean result = PasswordUtil.matches(user.getEncryptSalt(), oldPassword, user.getPassword());
            if (result) {
                //获取新密码
                String NewPassword = userUpdatePasswordVO.getNewPassword();
                //RSA解密
                String decodePassword = RsaEncryptUtil.privateKeyDecrypt(NewPassword);
                //获取加密盐
                String salt = PasswordUtil.getSalt();
                //使用MD5加密密码后用于存入数据库
                String password = PasswordUtil.encode(salt, decodePassword);

                int res = userMapper.updatePassword(id, salt, password);
                if (res > 0) {
                    return "密码修改成功";
                }
                return "密码修改失败";
            }
            return "旧密码不正确";
        }
        return "用户不存在";
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User findUserByPhone(String phone) {
        return userMapper.findUserByPhone(phone);
    }

    @Override
    public PageResult listAllUserForManager(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());

        //获取查询参数
        String phone = Convert.toStr(pageRequest.getParamByKey("phone"));
        List<UserDTO> userList = userMapper.listAllUserForManager(phone);

        PageResult result = PageHelperUtil.pageResult(new PageInfo<>(userList));
        return result;
    }
}
